<template>
  <div class="app-container">
    <el-form :model="formData" label-width="120px" label-position="left">
      <el-form-item label="年份">
        <el-select v-model="formData.year" placeholder="请选择年份">
          <el-option
            v-for="item in yearList"
            :key="item"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="科目">
        <el-select v-model="formData.subject" placeholder="请选择科目">
          <el-option
            v-for="item in subjectList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分数">
        <el-input v-model="formData.score"></el-input>
      </el-form-item>
      <el-form-item label="位次区间">
        <el-input v-model="formData.section"></el-input>
      </el-form-item>
      <el-form-item label="同分人数">
        <el-input v-model="formData.scoreNum"></el-input>
      </el-form-item>
    </el-form>
    <div class="submit-box">
      <el-button type="primary" :loading="loading" style="width: 200px;" @click.native.prevent="doSubmit">提交</el-button>
    </div>
  </div>
</template>

<script>
  import { edit, getDetail } from '@/api/rank'
  import { errorMsg, successMsg } from '@/utils/my'

  export default {
    data() {
      return {
        loading: false,
        formData: {
          id: 0,
          year: '',
          subject: '',
          score: 0,
          scoreNum: 0,
          section: ''
        },
        yearList: [],
        subjectList: [
          {id: 1, name: '文科'},
          {id: 2, name: '理科'}
        ]
      }
    },

    created() {
      this.formData.id = this.$route.params.id
      this.genYearList()
      this.fetchData()
    },

    methods: {

      fetchData() {
        const that = this
        getDetail({id: that.formData.id}).then(res => {
          that.formData.year = res.data.rankYear
          that.formData.subject = res.data.rankSubject
          that.formData.score = res.data.rankScore.toString()
          that.formData.scoreNum = res.data.rankScoreNum.toString()
          that.formData.section = res.data.rankSection.toString()
        })
      },

      doSubmit() {
        let that = this,
            d = that.formData
        if (d.year.length <= 0) {
          return errorMsg('请选择年份')
        }
        if (d.subject.length <= 0) {
          return errorMsg('请选择科目')
        }
        if (d.score.length <= 0) {
          return errorMsg('请输入分数')
        }
        if (d.section.length <= 0) {
          return errorMsg('请输入位次区间')
        }
        if (d.scoreNum.length <= 0) {
          return errorMsg('请输入同分人数')
        }
        that.loading = true
        edit(d).then(res => {
          that.loading = false
          if (res.code != 20000) {
            return errorMsg(res.message)
          }
          successMsg(res.message)
          setTimeout(function() {
            that.$router.push({path: '/admin/rank/list'})
          }, 1500)
        })
      },

      genYearList() {
        const that = this
        let d = new Date(),
            curYear = d.getFullYear()
        for (let i = 0; i < 5; i++) {
          that.yearList.push(curYear)
          curYear--
        }
      }

    }
  }
</script>

<style>
  .submit-box {
    height: 50px;
    line-height: 50px;
    text-align: center;
    padding-right: 20px;
  }
</style>
